# -*- coding: utf-8 -*-

'''eof
name:申请人居住状况
code:Liv_status
tableName:
columnName:
groups:个人通用
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import datetime
from dateutil.relativedelta import relativedelta
import json

reload(sys)
sys.setdefaultencoding('utf-8')

'''
申请人居住状况 
    提取个人征信报告-居住信息中的“居住状况”字段
'''
def FormatData(tableName):
    try:
        icr = json.loads(IC[0]["icr"])[tableName]
        return icr if isinstance(icr, list) and len(icr) > 0 else [{}]
    except:
        return [{}]

def Liv_status():
    null_type_list = ['', None, 'null', 'Null', '/', ' ']
    LivStatus = ['自置', '共有住宅', '按揭','亲属楼宇' ,'租房','集体宿舍' ,'未知' ,'其他']
    try:
        # 居住信息
        ICRResidence = FormatData("ICRResidence")
        if ICRResidence == [{}] :
            return u"缺失值"
        # 居住状况
        if ICRResidence[0].get("ResidenceType") not in null_type_list \
                and ICRResidence[0].get("ResidenceType") in LivStatus:
            return ICRResidence[0].get("ResidenceType")
        else:
            return u"缺失值"
    except:
        return u'缺失值'

result = Liv_status()

